/*
 * Copyright (c)e 2024-2024.Powered by 清明雨揽月
 * This software is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN 'AS IS' BASIS, WITHOUT WARRANTIES OF ANY
 * KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
 * NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include "sequence_stack.h"
#include "iostream"

using namespace std;

/**
 * operate():关于顺序栈的基本操作
 */
void sequence_stack::operate()
{
    init_stack();
    stack_empty();
    for (int index = 0; index < sizeof(arr); ++index)
    {
        push(arr[index]);
    }
    stack_empty();
    get_top();
    pop();
    get_top();
}

/**
 * init_stack():初始化栈
 */
void sequence_stack::init_stack()
{
    _sq_stack.top = -1;
    cout << "栈初始化成功" << endl;
}

/**
 * stack_empty():判断栈是否为空
 * @return
 */
bool sequence_stack::stack_empty() const
{
    if (_sq_stack.top == -1)
    {
        cout << "栈为空" << endl;
        return true;
    }
    else
    {
        cout << "栈不为空" << endl;
        return false;
    }
}

/**
 * push(int data):元素入栈
 * 入栈操作，若栈顶指针初始化不为-1，为0的情况下，
 * _sq_stack指向栈顶元素的下一位置，入栈操作变为_sq_stack.data[_sq_stack.top++]=data
 * @param data
 * @return
 */
bool sequence_stack::push(int data)
{
    if (_sq_stack.top == MAX_SIZE - 1)
        return false;
    _sq_stack.data[++_sq_stack.top] = data;
    cout << "数据为" << data << "的元素入栈" << endl;
    return true;
}

/**
 * pop():元素出栈
 * 入栈操作，若栈顶指针初始化不为-1，为0的情况下，
 * _sq_stack指向栈顶元素的下一位置，出栈操作变为data=_sq_stack.data[--_sq_stack.top]
 * @return
 */
bool sequence_stack::pop()
{
    if (_sq_stack.top == -1)
        return false;
    int data = _sq_stack.data[_sq_stack.top--];
    cout << "数据为" << data << "的元素出栈" << endl;
    return true;
}
/**
 * get_top():获取栈顶元素
 * @return
 */
bool sequence_stack::get_top() const
{
    if (_sq_stack.top == -1)
        return false;
    int data = _sq_stack.data[_sq_stack.top];
    cout << "栈顶元素为：" << data << endl;
    return true;
}

/**
 * destroy_stack():销毁栈
 */
void sequence_stack::destroy_stack()
{
    _sq_stack.top = -1;
}
